{% extends "oa/base.html" %}
{% block this_css %} {% endblock %}
{% block title %} 生产批次成本计算 {% endblock %}

{% block content %}
<div class="container">
    <div class="row">
        {% if data %}
            <div class="col-12">
            <h3>基础信息</h3>

            <table class="table">
                <tr><td>产品批次</td><td>{{ data.batch_code }}</td><td colspan="3"></td></tr>
                <tr><td>投料日期</td><td>{{ data.start_date }}</td><td colspan="3"></td></tr>
                <tr><td>产品名称</td><td>{{ data.product_name }}</td><td colspan="3"></td></tr>
                <tr><td>生产状态</td><td>{{ data.product_state }}</td><td colspan="3"></td></tr>
                <tr><td>发酵罐</td><td>{{ data.current_tank }}</td><td colspan="3"></td></tr>
                <tr><td>批次容量</td><td>{{ data.total_volume }} L</td><td colspan="3"></td></tr>
                <tr><td>加工费</td><td>{{ data.total_processing_costs|floatformat:"2" }} 元</td><td colspan="3"></td></tr>
                <tr><th>预估</th><td>出酒量</td><td>{{ data.est_volume|floatformat }} L</td><td>每升原料成本（元）</td><td><b>{{ data.est_material_cost_per_liter|floatformat:"2" }}</b></td></tr>
                <tr><th>实际</th><td>出酒量</td><td>{{ data.act_volume|floatformat }} L</td><td>每升原料成本（元）</td><td>{{ data.act_material_cost_per_liter|floatformat:"2" }}</td></tr>
            </table>

            <h3>原料</h3>
            <table class="table table-striped table-bordered table-responsive">
                <thead>
                    <tr>
                        <th>日期</th>
                        <th>出库单号</th>
                        <th>原料批次号</th>
                        <th>原料编号</th>
                        <th>原料名称</th>
                        <th class="text-center">单价（元）</th>
                        <th class="text-center">数量</th>
                        <th class="text-center">小计（元）</th>
                    </tr>
                </thead>
                <tbody>
                    {% for m in data.materials %}
                        <tr>
                            <td>{{ m.date|date:'Y-m-d' }}</td>
                            <td>{{ m.order_no }}</td>
                            <td>{{ m.batch_code }}</td>
                            <td>{{ m.material_code }}</td>
                            <td>{{ m.material_name }}</td>
                            <td class="text-center">{{ m.unit_cost|floatformat:"2" }}</td>
                            <td class="text-center">{{ m.amount }} {{ m.unit }}</td>
                            <td class="text-center ">{{ m.cost|floatformat:"2" }}</td>
                        </tr>
                    {% endfor %}
                    <tr><th>合计：</th><td colspan="6"></td><td class="text-center"><b>{{ data.total_material_costs|floatformat:"2" }}</b></td></tr>
                </tbody>
            </table>
            <h3>灌装记录</h3>
            <table class="table table-striped table-bordered table-responsive">
                <thead>
                    <tr>
                        <th>日期</th>
                        <th>灌装批次号</th>
                        <th>产品名称</th>
                        <th class="text-center">灌装数量</th>
{#                        <th class="text-center">灌装容量（升）</th>#}
                        <th class="text-center">预估成本（元）</th>
                        <th class="text-center">实际成本（元）</th>
                        <th class="text-center">包材</th>
                    </tr>
                </thead>
                <tbody>
                {% for pack in data.packs %}
                    <tr>
                        <td>{{ pack.date|date:'Y-m-d' }}</td>
                        <td>{{ pack.code }}</td>
                        <td>{{ pack.name }}</td>
                        <td class="text-center">{{ pack.pack_amount }}</td>
{#                        <td class="text-center">{{ pack.liter|floatformat:"2" }}</td>#}
                        <td class="text-center"><b>{{ pack.est_per_pack_cost|floatformat:"2" }}</b></td>
                        <td class="text-center">{{ pack.act_per_pack_cost|floatformat:"2" }}</td>
                        <td>
                            <p>包材均摊成本：{{ pack.unit_pack_cost|floatformat:"2" }} 元</p>
                            <ul>
                            {% for pm in pack.pack_materials %}
                                <li>{{ pm.material_code }} {{ pm.material_name }} ({{ pm.material_batch_code }}) <br>[<b>{{ pm.amount|floatformat:"-2" }} * {{ pm.material_batch_unit_cost|default_if_none:"0"|floatformat:"-2" }} = {{ pm.material_batch_cost|default_if_none:"0"|floatformat:"-2" }}</b>]</li>
                            {% endfor %}
                            </ul>
                            <p>包材总成本：{{ pack.pack_cost|floatformat:"2" }} 元</p>
                        </td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>
            <h3>组合记录</h3>
            <table class="table table-striped table-bordered">
                <thead>
                    <tr>
                        <th>日期</th>
                        <th>组合批次号</th>
                        <th>组合名称</th>
                        <th class="text-center">组合数量</th>
                        <th class="text-center">预估成本（元）</th>
                        <th class="text-center">实际成本（元）</th>
                        <th class="text-center">产品</th>
                        <th class="text-center">包材</th>
                    </tr>
                </thead>
                <tbody>
                {% for group in data.groups %}
                    <tr>
                        <td>{{ group.date|date:'Y-m-d' }}</td>
                        <td>{{ group.code }}</td>
                        <td>{{ group.package_group_name }}</td>
                        <td class="text-center">{{ group.amount }}</td>
                        <td class="text-center"><b>{{ group.est_per_group_cost|floatformat:"2" }}</b></td>
                        <td class="text-center">{{ group.act_per_group_cost|floatformat:"2" }}</td>
                        <td>
                            <p>预估产品均摊成本：{{ group.est_per_pack_item_cost|floatformat:"2" }} 元</p>
                            <p>实际产品均摊成本：{{ group.act_per_pack_item_cost|floatformat:"2" }} 元</p>
                            <ul>
                                {% for gb in group.group_pack_batches %}
                                <li>{{ gb.pack_batch__code }} {{ gb.pack_batch__product_package__name }} {{ gb.amount }}</li>
                                {% endfor %}
                            </ul>
                        </td>
                        <td>
                            <p>包材均摊成本：{{ group.per_group_pack_cost|floatformat:"2" }} 元</p>
                            <ul>
                            {% for gm in group.group_materials %}
                                <li>{{ gm.material_code }} {{ gm.material_name }} ({{ gm.material_batch_code }}) <br>[<b>{{ gm.amount|floatformat:"-2" }} * {{ gm.material_batch_unit_cost|default_if_none:"0"|floatformat:"-2" }} = {{ gm.material_batch_cost|default_if_none:"0"|floatformat:"-2" }}</b>]</li>
                            {% endfor %}
                            </ul>
                            <p>包材总成本：{{ group.group_pack_cost|floatformat:"2" }} 元</p>
                        </td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>
        </div>
        {% else %}
            <div class="col-12"><h3>你来自何方？</h3></div>
        {% endif %}
    </div>
</div>
{% endblock %}